/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.example.web;

import com.example.model.*;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Sreenath
 */
public class LoginServlet extends HttpServlet {
	
	/**
	 * Validates username password before passing to the model class
	 * 
	 * @param uname
	 * @param passwd
	 * @return
	 */
	private boolean validate(String uname, String passwd){
		if(uname.isEmpty() || passwd.isEmpty()){
			return false;
		}
		else 
			return true;
		//Needed to add more validation checks here..
	}
   
	
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    public void doPost(HttpServletRequest request,
						HttpServletResponse response)
						throws IOException, ServletException {

	String uname=request.getParameter("uname");
	String pass=request.getParameter("passwd");
	
	//Validation check for username and password
	if(!validate(uname, pass)){
		//raise login credentails validation error
	}
		
	else {
		UserCheck ucheck = new UserCheck();
		String flag = ucheck.checkUser(uname, pass);
		request.setAttribute("styles", flag);
	}

	javax.servlet.RequestDispatcher view=request.getRequestDispatcher("index.jsp");
	view.forward(request,response);
    }

  
}